package com.dean.ui.panel.impl;

import com.dean.action.DeanJobAction;
import com.dean.batch.entity.DeanTree;
import com.dean.constants.DeanUiConstants;
import com.dean.entity.DataQueryEntity;
import com.dean.entity.DeanResponse;
import com.dean.ui.panel.DeanPanel;
import com.dean.ui.utils.UiUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @ClassName DbQueryPanel
 * @Description sql查询页面
 * @Author dean
 * @Date 2021/10/3 10:03
 */
public class DeanReplaceFileCharPanel implements DeanPanel{

	private static final Logger logger = LoggerFactory.getLogger(DeanReplaceFileCharPanel.class);
    /**
     * 配置 文件路径
     */
    private final static String INIT_PATH_NAME = DeanUiConstants.INIT_PATH + "repfilechar.properties";

    private  DataQueryEntity dataQueryEntity;
    private  JTextField executeRes;
    private  JTree tree;
    private DefaultTreeModel treeModel;
    private DefaultMutableTreeNode root = new DefaultMutableTreeNode("流程展示");

    private  JTable table;

	private  JPanel panel = null;
    @Override
	public  JPanel showView(){
		// 初始化 panel
		panel = new JPanel();
        leftUi();
		return panel;
	}

    @Override
    public JScrollPane showScrollView() {
        return null;
    }

    private void leftUi(){
        //添加新的panel
        panel.setBounds(UiUtils.menuWidth, 0, UiUtils.contentWidth,UiUtils.noBorderHeight);
        panel.setBorder(BorderFactory.createLineBorder(Color.darkGray,1));
        panel.setLayout(null);
        // sql输入窗口
        treeModel = new DefaultTreeModel(root);
        tree = new JTree(treeModel);
        tree.setBounds(UiUtils.elePadding*3, UiUtils.inputPadding, UiUtils.contentWidth - UiUtils.elePadding*4, 1000);

        JScrollPane treePane = new JScrollPane(tree);
        treePane.setBounds(UiUtils.elePadding*2, UiUtils.inputPadding, UiUtils.contentWidth - UiUtils.elePadding*2, (UiUtils.menuItemHeight*6+UiUtils.inputPadding*5));
        panel.add(treePane);

        // 查询列表窗口
        table = new JTable();
        JScrollPane tablelPane = new JScrollPane(table);
        tablelPane.setBounds(UiUtils.elePadding, (UiUtils.inputPadding*8+UiUtils.menuItemHeight*7), UiUtils.contentWidth - UiUtils.elePadding*2, UiUtils.noBorderHeight-(UiUtils.inputPadding*8+UiUtils.menuItemHeight*7));
        panel.add(tablelPane);


        JButton exportBtn = new JButton("查看关系图");
        exportBtn.setBounds(UiUtils.elePadding*2+UiUtils.labelWidth, (UiUtils.inputPadding*7+UiUtils.menuItemHeight*6), UiUtils.labelWidth, UiUtils.menuItemHeight);

        panel.add(exportBtn);

        exportBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取table选中
                int rownum = table.getSelectedRow();
                if(rownum == -1){
                    JOptionPane.showMessageDialog(panel, "请选中一条记录", "WARNING", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                String jobId = table.getValueAt(rownum,0).toString();
                root.setUserObject(jobId);
                root.removeAllChildren();
                // 查询该任务的所有下行节点
                getChild(root);
                treeModel.reload();
                tree.setVisible(true);
            }
        });

        // 执行按钮
        JButton importBtn = new JButton("查询");
        importBtn.setBounds(UiUtils.elePadding, (UiUtils.inputPadding*7+UiUtils.menuItemHeight*6), UiUtils.labelWidth, UiUtils.menuItemHeight);
        panel.add(importBtn);
        importBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                queryExecute();
            }
        });

        JLabel executeLabel = new JLabel("提示信息:");
        executeLabel.setBounds(UiUtils.elePadding*3+UiUtils.labelWidth*2, (UiUtils.inputPadding*7+UiUtils.menuItemHeight*6), UiUtils.labelWidth, UiUtils.menuItemHeight);
        panel.add(executeLabel);

        executeRes = new JTextField("");
        executeRes.setBounds(UiUtils.elePadding*3+UiUtils.labelWidth*3, (UiUtils.inputPadding*7+UiUtils.menuItemHeight*6), (UiUtils.contentWidth-(UiUtils.elePadding*4+UiUtils.labelWidth*3)), UiUtils.menuItemHeight);
        executeRes.setForeground(Color.RED);
        Font font = new Font("黑体", Font.PLAIN, 20);
        executeRes.setFont(font);
        executeRes.setEditable(false);
        panel.add(executeRes);
        executeRes.setColumns(10);

    }


    private void getChild(DefaultMutableTreeNode node){
        DeanResponse<DeanTree> response = DeanJobAction.getInstance().queryChild(node.getUserObject().toString());
        if(response.getCode() == 0){
            DeanTree deanTree = response.getData();
            createTree(deanTree,node);

        }else{
            JOptionPane.showMessageDialog(panel, response.getMsg(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void createTree(DeanTree root,DefaultMutableTreeNode node){
        java.util.List<DeanTree> childs = root.getChild();
        if(childs != null && !childs.isEmpty()){
            for (DeanTree child : childs) {
                DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child.getName());
                node.add(childNode);
                createTree(child,childNode);
            }
        }
    }

	private void queryExecute(){
        dataQueryEntity = DeanJobAction.getInstance().query();
        if(dataQueryEntity.getCode() == 0){
            DefaultTableModel tableModel = new DefaultTableModel(dataQueryEntity.getDatas(),dataQueryEntity.getHeader());
            table.setModel(tableModel);
        }
        executeRes.setText(dataQueryEntity.getCode()+"==="+dataQueryEntity.getMsg());
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.setVisible(true);
    }
}
